function updateagentpath(k)

global m cells agents costs velocities gi gj gcosti gcostj

ay = agents(1,k);
ax = agents(2,k);

ai = round(ay);
aj = round(ax);

if ai < 2
    ai = 2;
end

if aj < 2
    aj = 2;
end

if ai > m-2
    ai = m-2;
end

if aj > m-2
    aj = m-2;
end


%gi = cells(ai+1,aj) - cells(ai-1,aj);
%gj = cells(ai,aj+1) - cells(ai,aj-1);

ddai = rand()-.5 + 10*gj(ai,aj);
ddaj = rand()-.5 + 10*gi(ai,aj);

cells(ai,aj) = cells(ai,aj) + 1;

velocities(1,k) = velocities(1,k) - ddai;
velocities(2,k) = velocities(2,k) - ddaj;

norm = velocities(1,k)^2 + velocities(2,k)^2;
velocities(1,k) = velocities(1,k) / norm;
velocities(2,k) = velocities(2,k) / norm;

ai = ai + velocities(1,k);
aj = aj + velocities(2,k);

if ai < 2
    ai = 2;
end

if aj < 2
    aj = 2;
end

if ai > m-2
    ai = m-2;
end

if aj > m-2
    aj = m-2;
end

% collide with obstacles
if costs(round(ai),round(aj)) > 0
    cells(ai,j) = cells(ai,aj) - 5;
    
    ai = ai - velocities(1,k);
    aj = aj - velocities(2,k);
end


agents(1,k) = ai;
agents(2,k) = aj;


end
